package cn.wizzer.app.sys.modules.services.impl;

import cn.wizzer.app.sys.modules.models.SysCollectCard;
import cn.wizzer.app.sys.modules.models.vo.SysCollectCardVO;
import cn.wizzer.app.sys.modules.services.SysCollectCardService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.base.service.CommonDBService;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.ArrayUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.plugins.wkcache.annotation.CacheDefaults;
import org.nutz.plugins.wkcache.annotation.CacheRemoveAll;

import java.util.List;
import java.util.Random;

@IocBean(args = {"refer:dao"})
@Service(interfaceClass = SysCollectCardService.class)
@CacheDefaults(cacheName = "sys_collect_card")
public class SysCollectCardServiceImpl extends BaseServiceImpl<SysCollectCard> implements SysCollectCardService {

    @Inject
    private CommonDBService dbService;

    public SysCollectCardServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Pagination listPage(Integer pageNumber, int pageSize, String searchKeyword, String[] searchDate) {
        // 根据收款人,银行卡号搜索
        Sql sql = Sqls.create("SELECT c.*,b.name AS bank,b.shortName,b.logo,sc.country,sc.logo as flag FROM sys_collect_card c ");
        Sql countSql = Sqls.create("SELECT COUNT(1) FROM sys_collect_card c ");

        String joinSql = "LEFT JOIN sys_bank_lib b ON b.id = c.bankLibId " +
                "LEFT JOIN sys_currency sc ON b.currencyId = sc.id "+
                "$condition ";

        sql.appendSourceSql(joinSql);
        countSql.appendSourceSql(joinSql);
        Cnd cnd = Cnd.NEW();
        cnd.and("c.delFlag","=",0);
        if (Strings.isNotBlank(searchKeyword)) {
            searchKeyword = "%" + searchKeyword + "%";
            cnd.and(Cnd.exps("b.name", "like", searchKeyword)
                    .or("c.no", "like", searchKeyword)
                    .or("c.accountName", "like", searchKeyword)
                    .or("sc.country", "like", searchKeyword));
        }
        if (ArrayUtils.isNotEmpty(searchDate)) {
            cnd.and("opAt", ">=", Times.d2TS(Times.D(searchDate[0])));
            cnd.and("opAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        sql.setCondition(cnd);
        sql.appendSourceSql("order by opAt desc");
        countSql.setCondition(cnd);
        return dbService.listPage(SysCollectCardVO.class,pageNumber,pageSize,sql.toString(),countSql.toString());
    }

    @Override
    public void add(SysCollectCard card) {
        card.setOpAt(1L);
        this.insert(card);
        this.clearCache();
    }

    @Override
    public void edit(SysCollectCard card) {
        this.update(card);
        this.clearCache();
    }

    @Override
    @CacheRemoveAll
    public void clearCache() {
    }

    @Override
    public SysCollectCard getRandomCard(String bankId) {
        List<SysCollectCard> list = this.query(
                Cnd.where("bankLibId", "=", bankId)
                    .and("delFlag","=",0)
                    .and("disabled","=",0)
        );
        if (list.size() == 0) {
            return null;
        }
        return list.get(new Random().nextInt(list.size()));
    }

}
